package thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Thread02 extends Thread{
    private String url;//图片网络地址
    private String name;//图片名称
    public Thread02(String url, String name){
        this.url=url;
        this.name=name;
    }
    //下载图片线程执行体
    @Override
    public void run() {//重写Run方法
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+name+"的文件");

    }

    public static void main(String[] args) {
        Thread02 t1 = new Thread02("https://pic4.zhimg.com/v2-34ebf1e09d2aa091f02fc0c80a440823_r.jpg?source=1940ef5c","a");
        Thread02 t2 = new Thread02("https://tse1-mm.cn.bing.net/th/id/OIP-C.aqeNZpJHANcKUY--8SnjPgHaHa?pid=ImgDet&rs=1","b");
        Thread02 t3 = new Thread02("https://tse1-mm.cn.bing.net/th/id/R-C.0b9e175e77c2939d166f9bc234161b2f?rik=K8LNaXVAa33uMw&riu=http%3a%2f%2fscimg.jianbihuadq.com%2ftouxiang%2f202002%2f2020021415012325.jpg&ehk=8bHHcCHySyUD26oPbbhbaAZQGx5n5wBUep3SQDWfT%2fM%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1","c");
        t1.start();
        t2.start();
        t3.start();
    }
    //下载器
    class WebDownloader{
        //下载方法
        public void downloader(String url,String name){
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
